﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lead2012.Business;
using Lead2012.Models;
using Lead2012.ViewModels;
using Lead2012.Repository;

namespace Lead2012.Controllers
{
    public class LeadFormTypeController : BaseController
    {
        // Create an instance of repository
        LeadFormTypeManagementRepository amRepository = new LeadFormTypeManagementRepository();

        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            LeadFormTypeViewModel model = new LeadFormTypeViewModel();

            // Setup model
            model.PageType = "Menu";
            SetupCommon(model, string.Empty, "Name");
            amRepository.GetModel(model, string.Empty, string.Empty, string.Empty, 1, 10);

            return View(model);
        }

        public ActionResult Filters(string keywords, string sortBy, string sortOrder, int pageIndex, int pageSize)
        {
            LeadFormTypeViewModel model = new LeadFormTypeViewModel();

            // Setup model
            if (pageSize == 0)
                pageSize = 10;
            if (pageIndex == 0)
                pageIndex = 1;

            amRepository.GetModel(model, keywords, sortBy, sortOrder, pageIndex, pageSize);

            return Json(model, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Delete(int id)
        {
            LeadFormTypeRepository repository = new LeadFormTypeRepository();
            LeadFormTypeViewModel model = new LeadFormTypeViewModel();

            // Delete action
            repository.Delete(id);

            // Setup model
            amRepository.GetModel(model, string.Empty, string.Empty, string.Empty, 1, 10);

            return Json(model, JsonRequestBehavior.AllowGet);
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult Add()
        {
            LeadFormTypeViewModel model = new LeadFormTypeViewModel();

            // Setup model
            model.PageType = "Menu";
            SetupCommon(model, string.Empty, "Name");
            amRepository.GetModel(model, 0);

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(FormCollection form)
        {
            LeadFormTypeRepository repository = new LeadFormTypeRepository();
            LeadFormType model = new LeadFormType();

            model.Enabled = (form["Enabled"] != null && form["Enabled"].ToString() == "on") ? true : false;
            model.Visible = (form["Visible"] != null && form["Visible"].ToString() == "on") ? true : false;
            model.Title = form["Title"].ToString();

            repository.Create(model);

            return Redirect("/LeadFormType");
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id)
        {
            LeadFormTypeViewModel model = new LeadFormTypeViewModel();

            // Setup model
            model.PageType = "Menu";
            SetupCommon(model, string.Empty, "Name");
            amRepository.GetModel(model, id);
            ViewBag.TypeId = id;

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection form)
        {
            LeadFormTypeRepository repository = new LeadFormTypeRepository();
            LeadFormType model = new LeadFormType();

            model = repository.GetSingle(id);

            model.Enabled = (form["Enabled"] != null && form["Enabled"].ToString() == "on") ? true : false;
            model.Visible = (form["Visible"] != null && form["Visible"].ToString() == "on") ? true : false;
            model.Title = form["Title"].ToString();

            repository.Update(model);

            return Redirect("/LeadFormType");
        }
    }
}
